package com.nisure.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.nisure.AutoKeywordApplication;
import com.nisure.common.CommonData;
import com.nisure.common.CreateJsonObject;
import com.nisure.common.GetHttpJsonobject;
import com.nisure.common.R;
import com.nisure.info.AdgroupTypeInfo;
import com.nisure.service.AdgroupService;
import com.nisure.type.AdgroupType;
import com.nisure.type.CampaignType;

@RestController
public class AdgroupController {
	
	private static final Logger logger = LoggerFactory.getLogger(AutoKeywordApplication.class);
	
	@Autowired
	private GetHttpJsonobject getHttpJsonobject;
	@Autowired
	private CreateJsonObject createJsonObject;
	@Autowired
	private AdgroupService adgroupService;

	public String addAdgroup(String accessToken,Long campaignId,String adgroupName){
		String url = "https://api.baidu.com/json/sms/v4/AdgroupService/addAdgroup";	
		//判断是否存在
		List<Long> campaignIdList = new ArrayList<Long>();
		JSONObject adgroupjson = this.getAllAdgroup(accessToken, campaignIdList);		
		adgroupjson = JSONObject.parseObject(adgroupjson.getString("body"));		
		JSONArray adgroupArr = JSONArray.parseArray(adgroupjson.getString("data"));
		String adgroupId = null;
		for(int i=0;i<adgroupArr.size();i++){
			JSONObject adgroupData = adgroupArr.getJSONObject(i);
			if(adgroupData.getString("adgroupName").equals(adgroupName)){//存在
				adgroupId = adgroupData.getString("adgroupId");
			}
		}if(adgroupId == null){//不存在，添加
			
			JSONObject paramsList = new JSONObject();
			List<JSONObject> jsonList = new ArrayList<JSONObject>();
			JSONObject jsonParams = new JSONObject();
			jsonParams.put("campaignId", campaignId);
			jsonParams.put("adgroupName", adgroupName);
			jsonParams.put("maxPrice", 100);
			jsonList.add(jsonParams);
			paramsList.put("adgroupTypes", jsonList);
			JSONObject completeJson = createJsonObject.getJsonData(accessToken, paramsList);
			JSONObject responsejson = getHttpJsonobject.httpsRequest(url, "POST", completeJson.toJSONString());			
			responsejson = JSONObject.parseObject(responsejson.getString("body"));
			JSONArray jsonArr2 = JSONArray.parseArray(responsejson.getString("data"));
			JSONObject datajson = new JSONObject();
			
			for(int i=0;i<jsonArr2.size();i++){
				datajson = jsonArr2.getJSONObject(i);					
				adgroupId = datajson.getString("adgroupId");
				
			}
			
		}
		return adgroupId;
		
	}
	public JSONObject getAllAdgroup(String accessToken,List<Long> campaignIdList){
		String url = "https://api.baidu.com/json/sms/v4/AdgroupService/getAdgroup";	
		JSONObject jsonParams = new JSONObject();
		String fields = "adgroupId,adgroupName";
		String[] str = fields.split(",");
		List<String> list = Arrays.asList(str);
		
		jsonParams.put("ids", campaignIdList);
		jsonParams.put("idType", 3);
		List<String> list2 = new ArrayList<String>();
		list2.add("adgroupId");
		list2.add("adgroupName");		
		jsonParams.put("adgroupFields", list);
		JSONObject completeJson = createJsonObject.getJsonData(accessToken, jsonParams);
		JSONObject adgroupjson = getHttpJsonobject.httpsRequest(url, "POST", completeJson.toJSONString());
		
		return adgroupjson;
	}
	public List<Long> saveAllAdgroup(String accessToken,List<Long> campaignIds,long userId){		
		
		
		JSONObject campaignjson = this.getAllAdgroup(accessToken, campaignIds);
		if(campaignjson.getString("body")==null){
			logger.info(campaignjson.toJSONString());
			return null;
		}
		JSONObject bodyjson = JSONObject.parseObject(campaignjson.getString("body"));
		if( JSONObject.parseArray(bodyjson.getString("data")).isEmpty()){
			logger.info(userId+"单元为空");
			return null;
		}
		JSONArray array = JSONObject.parseArray(bodyjson.getString("data"));
		List<AdgroupType> adgroupTypeList = JSON.parseArray(array.toJSONString(),AdgroupType.class);	
		List<AdgroupTypeInfo> adgroupTypeInfoList = new ArrayList<AdgroupTypeInfo>();
		List<Long> adgroupIdList = new ArrayList<Long>();
		for(AdgroupType a : adgroupTypeList){	
			adgroupIdList.add(a.getAdgroupId());
			AdgroupTypeInfo adgroupTypeInfo = new AdgroupTypeInfo();			
			BeanUtils.copyProperties(a,adgroupTypeInfo);
			adgroupTypeInfo.setPause(a.isPause()+"");//数据库不能存布尔类型			
			adgroupTypeInfo.setUserId(userId);
			adgroupTypeInfoList.add(adgroupTypeInfo);
		}
		int result = adgroupService.saveAllAdgroup(adgroupTypeInfoList);
		if(result>0){
			return adgroupIdList;
		}
		
		return null;
		
	}
}
